﻿using System.Text.Json;

namespace ClassLibrary_fqy_NET
{
    internal class JsonHelper
    {
        /// <summary>
        /// 导出数据到JSON文件
        /// </summary>
        /// <typeparam name="T">数据源类型</typeparam>
        /// <param name="data">数据源</param>
        /// <param name="filePath">文件路径</param>
        /// <param name="token">取消令牌</param>
        /// <returns></returns>
        public static async Task<bool> ExportToJsonAsync<T>(T data, string filePath, CancellationToken token = default)
        {
            if (!File.Exists(filePath))
            {
                return false;
            }
            var options = new JsonSerializerOptions
            {
                WriteIndented = true // This will format the JSON output
            };
            await using FileStream stream = File.Create(filePath);
            await JsonSerializer.SerializeAsync(stream, data, options, token);
            return true;
        }
        /// <summary>
        /// 导出数据到JSON文件
        /// </summary>
        /// <typeparam name="T">数据源类型</typeparam>
        /// <param name="data">数据源</param>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public static async Task<bool> ExportToJsonAsync<T>(T data, string filePath)
        {
            if (!File.Exists(filePath))
            {
                return false;
            }
            var options = new JsonSerializerOptions
            {
                WriteIndented = true // This will format the JSON output
            };
            await using FileStream stream = File.Create(filePath);
            await JsonSerializer.SerializeAsync(stream, data, options);
            return true;
        }
        /// <summary>
        /// 导出数据到JSON文件
        /// </summary>
        /// <typeparam name="T">数据源类型</typeparam>
        /// <param name="data">数据源</param>
        /// <param name="filePath">文件路径</param>
        /// <param name="options">配置选项</param>
        /// <returns></returns>
        public static async Task<bool> ExportToJsonAsync<T>(T data, string filePath, JsonSerializerOptions? options = default)
        {
            if (!File.Exists(filePath))
            {
                return false;
            }

            await using FileStream stream = File.Create(filePath);
            await JsonSerializer.SerializeAsync(stream, data, options);
            return true;
        }







        /// <summary>
        /// 从JSON文件导入数据
        /// </summary>
        /// <typeparam name="T">数据源类型</typeparam>
        /// <param name="filePath">文件路径</param>
        /// <param name="token">取消令牌</param>
        /// <returns></returns>
        public static async Task<T?> ImportInJsonAsync<T>(string filePath, CancellationToken token = default)
        {
            if (!File.Exists(filePath))
            {
                return default;
            }
            await using FileStream stream = File.OpenRead(filePath);
            return await JsonSerializer.DeserializeAsync<T>(stream, JsonSerializerOptions.Default, token);
        }
        /// <summary>
        /// 从JSON文件导入数据
        /// </summary>
        /// <typeparam name="T">数据源类型</typeparam>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public static async Task<T?> ImportInJsonAsync<T>(string filePath)
        {
            if (!File.Exists(filePath))
            {
                return default;
            }
            await using FileStream stream = File.OpenRead(filePath);
            return await JsonSerializer.DeserializeAsync<T>(stream);
        }
        /// <summary>
        /// 从JSON文件导入数据
        /// </summary>
        /// <typeparam name="T">数据源类型</typeparam>
        /// <param name="filePath">文件路径</param>
        /// <param name="options">配置选项</param>
        /// <returns></returns>
        public static async Task<T?> ImportInJsonAsync<T>(string filePath, JsonSerializerOptions? options = default)
        {
            if (!File.Exists(filePath))
            {
                return default;
            }
            await using FileStream stream = File.OpenRead(filePath);
            return await JsonSerializer.DeserializeAsync<T>(stream, options);
        }

    }
}
